#include "operationhelper.h"
#include <fstream>
#include <QTextCodec>
#include <QObject>
#include <QFile>

#define RED_FONT			"<font color=red>%1</font>"
#define BACKUP_FILE_SUFFIX	"%1.bak"

QTextCodec* detectEncoding(const QByteArray &bom)
{
	char utf8Bom[3] = {0xEF, 0xBB, 0xBF};
	char utf16BBom[2] = {0xFE, 0xFF};
	char utf16LBom[2] = {0xFF, 0xFE};
	char GB18030[4] = {0x84, 0x31, 0x95, 0x33};

	if (bom.indexOf(utf8Bom) > -1)
	{
		return QTextCodec::codecForName("UTF-8");
	}
	else if (bom.indexOf(utf16BBom) > -1)
	{
		return QTextCodec::codecForName("UTF-16BE");
	}
	else if (bom.indexOf(utf16LBom) > -1)
	{
		return QTextCodec::codecForName("UTF-16LE");
	}
	else if (bom.indexOf(GB18030) > -1)
	{
		return QTextCodec::codecForName("GB18030-0");
	}
	else 
	{
		return nullptr;
	}

}


int commitReplace(const QString &originalFilePath, const QString &tempFilePath)
{
	QString bakfilePath = QObject::tr(BACKUP_FILE_SUFFIX).arg(originalFilePath);

	QFile originalFile(originalFilePath);
	QFile tempFile(tempFilePath);

	if (!originalFile.rename(bakfilePath))
	{
		return -1;
	} 
	else if (!tempFile.rename(originalFilePath))
	{
		QFile backupFile(bakfilePath);
		backupFile.rename(originalFilePath); // God bless, if this operation failed...
		return -2;
	}

	return 0;
}


QString tripControlCharacter(
	const QString::const_iterator &lineStart, 
	const QString::const_iterator &lineEnd, 
	const QString::const_iterator &resultHead, 
	const QString::const_iterator &resultTail)
{
	QString::const_iterator forward = resultHead;
	QString::const_iterator backward = resultTail;
	QChar b, f;
	int i;
	for (i = 0; forward != lineStart && i < 80; forward--, i++)
	{
		f = *forward;
		if (f < 32)
		{ // remvoe control character
			forward++;
			break;
		}
	}
	for (i = 0; backward != lineEnd && i < 80; backward++, i++)
	{
		b = *backward;
		if (b < 32)
		{ // remvoe control character
			break;
		}
	}
	return QString::fromRawData(forward, backward - forward);
}

QString tripControlCharacterDisp(
	const QString::const_iterator &lineStart, 
	const QString::const_iterator &lineEnd, 
	const QString::const_iterator &resultHead, 
	const QString::const_iterator &resultTail)
{
	QString::const_iterator forward = resultHead;
	QString::const_iterator backward = resultTail;

	QString matchContent = QString::fromRawData(resultHead, resultTail - resultHead);
	QString match = QObject::tr(RED_FONT).arg(matchContent);

	QChar b, f;
	int i;
	for (i = 0; forward != lineStart && i < 80; forward--, i++)
	{
		f = *forward;
		if (f < 32)
		{ // remvoe control character
			forward++;
			break;
		}
	}
	for (i = 0; backward != lineEnd && i < 80; backward++, i++)
	{
		b = *backward;
		if (b < 32)
		{ // remvoe control character
			break;
		}
	}
	return QString::fromRawData(forward, resultHead - forward)
		.append(match)
		.append(QString::fromRawData(resultTail, backward - resultTail));
}